<template>
	<view class="content">
		<view class="text-shuru">
			<u--text text="输入验证码" size="20px" margin="50rpx 0px 0px 0rpx" bold></u--text>
		</view>
		<view class="text-yanzheng">
			<u--text :text="email" size="13px" margin="20rpx 0px 30px 0rpx" bold></u--text>
		</view>
		<view class="code">
			<u-code-input v-model="phoneCode" :maxlength="6" @finish="finish"></u-code-input>
		</view>
		<view class="text-weishou">
			<u--text type="warning" text="未收到验证码？" @click="sendEmailCode"></u--text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phoneCode: "", //验证码
				email: '验证码已发送至邮箱', //邮箱
			}
		},
		onLoad(options) {
			this.phone = options.phone
		},
		methods: {
			//验证码输入完成后的业务
			finish() {
				console.log(this.phoneCode)
				this.$myRequest({
					url: '/login/loginByPhoneCode',
					method: 'get',
					data: {
						'phone': this.phone,
						'code': this.phoneCode,
						'codeMark':'rider'
					}
				}).then(res=>{
					console.log(res)
					uni.setStorageSync('token',res.data.obj.token)
					uni.setStorageSync('id',res.data.obj.user.id)
					uni.setStorageSync('phone',res.data.obj.user.phone)
					uni.setStorageSync('loginName',res.data.obj.user.loginName)
					uni.setStorageSync('userName',res.data.obj.user.userName)
					uni.setStorageSync('avatarId',res.data.obj.avatarId)
					if(res.data.obj.user.role===1){
						uni.navigateTo({
							url: '/pages/zhuceqishou/zhuceqishou',
						})
					}
					if(res.data.obj.user.role===3){
						uni.navigateTo({
							url: '/pages/workview/orderdisplay/orderdisplay?phone=' + this.phone,
						})
						uni.getLocation({  
						    type: 'wgs84',
							geocode:true,
						    success: function (res) {  
						        console.log('当前位置的经度：' + res.longitude);  
						        console.log('当前位置的纬度：' + res.latitude);
								uni.setStorageSync("riderLng",res.longitude);
								uni.setStorageSync("riderLat",res.latitude)
						    }
						})
					}
					if(res.data.obj.user.role==2){
						this.$refs.uNotify.show({
						            top: 40,
						            type: 'error',
						            color: '#F8F9F9',
						            bgColor: '#F56C6C',
						            message: '您已经是商家了',
						            duration: 1000 * 3,
						            fontSize: 12,
						            safeAreaInsetTop:true
						        })
					}
				})
			},
			//重新发送验证码
			sendEmailCode() {
				this.$myRequest({
					url: '/shortMessage/SendVerificationByPhone',
					method: 'get',
					data: {
			 		'phone': this.phone,
					'codeMark':'rider'
					}
				})
				setTimeout(() => {
					uni.hideLoading();
					// 这里此提示会被this.start()方法中的提示覆盖
					uni.$u.toast('验证码已发送');
				}, 2000);
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.text-weishou {
		margin-left: -320rpx;
		margin-top: 50rpx;
	}

	.text-shuru {
		letter-spacing: 3rpx;
		font-family: KaiTi;
		margin-left: -180px;
		margin-top: 40px;
	}

	.text-yanzheng {
		letter-spacing: 3rpx;
		font-family: KaiTi;
		margin-left: -160px;
	}

	.code {
		margin-top: 10rpx;
	}
</style>
